package com.yeskery.nut.extend.mcp.server.method;

import com.yeskery.nut.bean.ApplicationContext;
import com.yeskery.nut.core.Environment;
import com.yeskery.nut.extend.mcp.core.Constants;
import com.yeskery.nut.extend.mcp.core.JsonMessage;
import com.yeskery.nut.extend.mcp.core.JsonRequest;
import com.yeskery.nut.extend.mcp.core.JsonResponse;
import com.yeskery.nut.extend.mcp.core.schema.InitializeSchema;
import com.yeskery.nut.extend.mcp.server.McpServerCapability;
import com.yeskery.nut.extend.mcp.server.McpServerSession;
import com.yeskery.nut.util.StringUtils;

import java.util.Arrays;

/**
 * 初始化请求处理
 * @author sprout
 * 2025-04-17 15:20
 */
public class InitializeMethodHandler extends BaseServerMethodHandler {

    /** 环境对象 */
    private final Environment environment;

    /**
     * 构造初始化请求处理
     * @param applicationContext  应用上下文
     * @param mcpServerCapability 服务能力
     * @param environment 环境对象
     */
    public InitializeMethodHandler(ApplicationContext applicationContext, McpServerCapability mcpServerCapability, Environment environment) {
        super(applicationContext, mcpServerCapability);
        this.environment = environment;
    }

    @Override
    protected void handleJsonResponse(McpServerSession serverSession, JsonMessage jsonMessage, JsonResponse jsonResponse) {
        JsonRequest jsonRequest = (JsonRequest) jsonMessage;
        InitializeSchema.Request request = converFromObject(jsonRequest.getParams(), InitializeSchema.Request.class);
        InitializeSchema.Response response = new InitializeSchema.Response();
        InitializeSchema.ServerCapabilities serverCapabilities = getServerCapabilities();
        response.setCapabilities(serverCapabilities);
        if (Arrays.asList(Constants.SUPPORT_PROTOCOL_VERSION_LIST).contains(request.getProtocolVersion())) {
            response.setProtocolVersion(request.getProtocolVersion());
        } else {
            response.setProtocolVersion(Constants.LATEST_PROTOCOL_VERSION);
        }
        InitializeSchema.Implementation serverInfo = new InitializeSchema.Implementation();
        serverInfo.setName(getServerName());
        serverInfo.setVersion(getVersion());
        response.setServerInfo(serverInfo);
        response.setInstructions(getDescription());
        jsonResponse.setResult(response);
    }

    /**
     * 获取服务能力
     * @return 服务能力
     */
    private InitializeSchema.ServerCapabilities getServerCapabilities() {
        InitializeSchema.ServerCapabilities serverCapabilities = new InitializeSchema.ServerCapabilities();
        InitializeSchema.ToolCapabilities toolCapabilities = new InitializeSchema.ToolCapabilities();
        toolCapabilities.setListChanged(true);
        serverCapabilities.setTools(toolCapabilities);
        InitializeSchema.ResourceCapabilities resourceCapabilities = new InitializeSchema.ResourceCapabilities();
        resourceCapabilities.setListChanged(true);
        resourceCapabilities.setSubscribe(true);
        serverCapabilities.setResources(resourceCapabilities);
        InitializeSchema.PromptCapabilities promptCapabilities = new InitializeSchema.PromptCapabilities();
        promptCapabilities.setListChanged(true);
        serverCapabilities.setPrompts(promptCapabilities);
        return serverCapabilities;
    }

    /**
     * 获取服务名称
     * @return 服务名称
     */
    private String getServerName() {
        String name = environment.getEnvProperty("nut.mcp.server.name");
        return StringUtils.isEmpty(name) ? "nut-mcp-server" : name;
    }

    /**
     * 获取版本号
     * @return 版本号
     */
    private String getVersion() {
        String version = environment.getEnvProperty("nut.mcp.server.version");
        return StringUtils.isEmpty(version) ? "1.0.0" : version;
    }

    /**
     * 获取描述
     * @return 描述
     */
    private String getDescription() {
        String description = environment.getEnvProperty("nut.mcp.server.description");
        return StringUtils.isEmpty(description) ? null : description;
    }
}
